import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  MaterialColor _themeColor = Colors.deepPurple;

  void _changeTheme(MaterialColor color) {
    setState(() {
      _themeColor = color;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: _themeColor),
        primarySwatch: _themeColor,
        iconTheme: IconThemeData(color: _themeColor),
      ),
      home: MyHomePage(
        title: 'Flutter Demo Home Page',
        onThemeChange: _changeTheme,
        themeColor: _themeColor,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
    required this.onThemeChange,
    required this.themeColor,
  });

  final String title;
  final ValueChanged<MaterialColor> onThemeChange;
  final MaterialColor themeColor;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  bool _isReverse = false;

  void _incrementCounter() {
    setState(() {
      _counter = _isReverse ? _counter - 1 : _counter + 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            SizedBox(height: 24),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => ThemeTestRoute(
                      themeColor: widget.themeColor,
                      onThemeChange: widget.onThemeChange,
                    ),
                  ),
                );
              },
              child: const Text('主题测试'),
            ),
            SizedBox(height: 16),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => CounterControlRoute(
                      counter: _counter,
                      onCounterChange: (newValue) {
                        setState(() {
                          _counter = newValue;
                        });
                      },
                      onReverseChange: (isReverse) {
                        setState(() {
                          _isReverse = isReverse;
                        });
                      },
                      initialReverse: _isReverse,
                    ),
                  ),
                );
              },
              child: const Text('计数器控制'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

// 修改ThemeTestRoute，支持回调
class ThemeTestRoute extends StatefulWidget {
  final MaterialColor themeColor;
  final ValueChanged<MaterialColor> onThemeChange;

  const ThemeTestRoute({
    required this.themeColor,
    required this.onThemeChange,
    Key? key,
  }) : super(key: key);

  @override
  _ThemeTestRouteState createState() => _ThemeTestRouteState();
}

class _ThemeTestRouteState extends State<ThemeTestRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("主题测试"),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            //第一行Icon保持默认颜色
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: const <Widget>[
                Icon(Icons.favorite),
                Icon(Icons.airport_shuttle),
                Text("  颜色不变")
              ],
            ),
            //为第二行Icon自定义颜色（固定为黑色)
            Theme(
              data: Theme.of(context).copyWith(
                iconTheme: Theme.of(context).iconTheme.copyWith(
                  color: Colors.black,
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const <Widget>[
                  Icon(Icons.favorite),
                  Icon(Icons.airport_shuttle),
                  Text("  颜色固定黑色")
                ],
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          final newColor = widget.themeColor == Colors.teal
              ? Colors.blue
              : Colors.teal;
          widget.onThemeChange(newColor);
        },
        child: const Icon(Icons.palette),
      ),
    );
  }
}

class CounterControlRoute extends StatefulWidget {
  final int counter;
  final ValueChanged<int> onCounterChange;
  final ValueChanged<bool> onReverseChange;
  final bool initialReverse;

  const CounterControlRoute({
    required this.counter,
    required this.onCounterChange,
    required this.onReverseChange,
    required this.initialReverse,
    Key? key,
  }) : super(key: key);

  @override
  _CounterControlRouteState createState() => _CounterControlRouteState();
}

class _CounterControlRouteState extends State<CounterControlRoute> {
  late int _counter;
  bool _isReverse = false;

  @override
  void initState() {
    super.initState();
    _counter = widget.counter;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('计数器控制'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            // 滑动条控制计数器值
            Slider(
              value: _counter.toDouble(),
              min: 0,
              max: 100,
              divisions: 100,
              label: _counter.round().toString(),
              onChanged: (double value) {
                setState(() {
                  _counter = value.toInt();
                });
                widget.onCounterChange(_counter);
              },
            ),
            // 开关控制计数方向
            SwitchListTile(
              title: const Text('反向计数'),
              value: _isReverse,
              onChanged: (bool value) {
                setState(() {
                  _isReverse = value;
                });
                widget.onReverseChange(value);
              },
            ),
          ],
        ),
      ),
    );
  }
}
